#include "Thread.h"
#include "CurrentThread.h"
#include <semaphore.h>

std::atomic_int Thread::numCreated_(0);

Thread::Thread(ThreadFunc func, const std::string &name)
    : started_(false), joined_(false), tid_(0), func_(std::move(func)), name_(name)
{
    setDefaultName();
}

Thread::~Thread()
{
    if (started_ && !joined_)
    {
        thread_->detach(); // thread类提供的设置分离线程的方法
    }
}

void Thread::start() // 一个Thread对象，记录的就是一个新线程的详细信息
{
    started_ = true;
    sem_t sem;
    sem_init(&sem, false, 0);

    // 开启新线程，新线程的执行逻辑
    thread_ = std::shared_ptr<std::thread>(new std::thread([&]()
                                                           {
        // 获取线程tid值
        tid_=CurrentThread::tid();
        // 发送信号，告知主线程“tid以获取”
        sem_post(&sem);  
        // 开启的新线程，专门执行该线程函数
        func_(); }));

    // 这里主线程必须等待获取上面创建的新线程的tid值
    sem_wait(&sem);
}

void Thread::join()
{
    joined_ = true;
    thread_->join();
}

// 生成默认线程名"Thread1""Thread2"
void Thread::setDefaultName()
{
    int num = ++numCreated_;
    if (name_.empty())
    {
        char buf[32] = {0};
        snprintf(buf, sizeof buf, "Thread%d", num);
        name_ = buf;
    }
}